/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package Model;

import DTO.Object.OrderUser.Orderuser1;
import DTO.Object.OrderUser.Orderusers;
import java.io.File;
import java.io.IOException;
import java.math.BigInteger;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;
import javax.xml.datatype.DatatypeConfigurationException;
import javax.xml.datatype.DatatypeFactory;
import javax.xml.datatype.XMLGregorianCalendar;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;

/**
 *
 * @author annt60258
 */
public class OrderUserSearch {
    List listorder=null;
   Orderusers orderusers;
   String idorder = "";
    public void SearchOrder(Node node, String search,String username)
    {
        if(node==null) return;
        if(node.getNodeName().equals("OrderID"))
        {
            idorder = node.getTextContent().trim();
        }
        if(node.getNodeName().equals("OrderDate"))
        {
            if(node.getTextContent().trim().indexOf(search)>-1)
            {
                try {
                    String dateorder = node.getTextContent().trim();
                    Node sibling = node.getNextSibling();
                    while (!sibling.getNodeName().equals("OrderStatus")) {
                        sibling = sibling.getNextSibling();
                    }
                    String statusorder = sibling.getTextContent().trim();
                    Orderuser1 orderuser1 = new Orderuser1();
                    orderuser1.setOrderID(new BigInteger(idorder));
                    orderuser1.setUserName(username);
                    XMLGregorianCalendar xmlgc = DatatypeFactory.newInstance().newXMLGregorianCalendar(dateorder);
                    orderuser1.setOrderDate(xmlgc);
                    orderuser1.setOrderStatus(Integer.parseInt(statusorder));
                    orderusers.getOrderuser().add(orderuser1);
                } 
                catch (DatatypeConfigurationException ex) {
                    Logger.getLogger(OrderUserSearch.class.getName()).log(Level.SEVERE, null, ex);
                }            }
        }
        NodeList nodelist = node.getChildNodes();
        for(int i =0;i<nodelist.getLength();i++)
        {
            SearchOrder(nodelist.item(i), search, username);
        }
    }
    public void controlsearch(String txtSeach,String username,String realpath)
    {
        try {
            listorder = new ArrayList();
            orderusers = new Orderusers();
            File file = new File(realpath);
            //goi ham marshall de co file xml
            marshallXML.marshallOrder(realpath, username);
            //parse xml thanh cay dom trc khi search
            DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
            DocumentBuilder db = dbf.newDocumentBuilder();
            Document doc = db.parse(file);
            //bat dau search
            SearchOrder(doc, txtSeach, username);
            //marshall lai thanh file xml
            JAXBContext jaxbcontext = JAXBContext.newInstance(orderusers.getClass());
            Marshaller mar = jaxbcontext.createMarshaller();
            mar.setProperty(Marshaller.JAXB_ENCODING, "UTF-8");
            mar.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
            mar.marshal(orderusers, file);
           
        } catch (SAXException ex) {
            Logger.getLogger(OrderUserSearch.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IOException ex) {
            Logger.getLogger(OrderUserSearch.class.getName()).log(Level.SEVERE, null, ex);
        } catch (JAXBException ex) {
            Logger.getLogger(OrderUserSearch.class.getName()).log(Level.SEVERE, null, ex);
        } catch (ParserConfigurationException ex) {
            Logger.getLogger(OrderUserSearch.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

}
